jQuery(function($){

  // edit quantity in cart items
  $("input#add_to_cart_items_quantity").keyup(function(e){
    var _this = this
  	var itemId = $(this).parent().data("id");
  	var currentNum = $("input.js-buy-num-"+ itemId).val();
  	var price = $("div#js-item-price-"+ itemId).text();
  	var storeId = getUrlParam("store_id");
  	var csrf = $("input#js-cart-item-index").val();
  	if ( e.which == 13 ) {
  	 e.preventDefault();
  	};
  	if(!isNumber(currentNum)){
  		// is not a number
      console.log("这里不是数字了");
  		showErrorView("请输入正确的数字!");
  		$.ajax({
  			// update quantity
  			url: "/api/cart_items/"+ itemId,
  			method: "put",
  			data: {quantity: 1, store_id: storeId},
  			headers: {
  								'X-CSRF-Token': csrf
  							},
  			success: function(data){
  				if(data.status == true){
  					$("input.js-buy-num-"+ itemId).val(1);
  					$("div#js-item-amount-"+ itemId).text(multiplied(data.price, 1));
  					$("span.js-cart-total-amount em").text("¥ "+ data.total_amount)
  				}
  			}
  		});//end ajax

  	}else {
  		// is a number
  		// var amount = price * currentNum * 10000 / 10000
  		$.ajax({
  			// update quantity
  			url: "/api/cart_items/"+ itemId,
  			method: "put",
  			data: {quantity: currentNum, store_id: storeId},
  			headers: {
  								'X-CSRF-Token': csrf
  							},
  			success: function(data){
  				if(data.status == true){
  					$("div#js-item-amount-"+ itemId).text(multiplied(data.price, currentNum));
  					$("span.js-cart-total-amount em").text("¥ "+ data.total_amount)
  				}
  			}
  		});//end ajax

  	}
  });
  // end edit cart items quantity





  //strat to remove cart item
  $("a.js-remove-cart-item").on("click", function(){
		var itemId = $(this).data("id");
		var csrf = $("input#js-cart-item-index").val();
		var storeId = getUrlParam("store_id");
		BootstrapDialog.show({
            title: '提示框:',
            message: "确定移除该商品?",
            buttons: [{
                label: '取消',
                action: function(dialog) {
                    dialog.close();
                }
            }, {
                label: '确定',
                cssClass: 'btn-primary',
                action: function(dialog){
									$.ajax({
										url: "/api/cart_items/"+ itemId +"/remove",
										method: "put",
										data: {store_id: storeId},
										headers: {
															'X-CSRF-Token': csrf
														},
										success: function(data){
											if(data.status == true){
												$("div.js-cart-item-div-"+ itemId).remove();
												$("span.js-cart-total-amount em").text("¥ "+ data.total_amount)
												dialog.close();
											}
										}
									})
                }
            }]
        });

	});
  // end to remove cart item

  $("a.js-to-order-new").on("click", function(e){
    console.log(getUrlParam("store_id"));
		var count = $(this).data("items");
    var customCount = $(this).data("custom-count");
		if (count <= 0){
			e.preventDefault();
			showErrorView("购物车为空，请选购产品!");
			return false
		}else if (customCount > 0){
      BootstrapDialog.show({
              title: '提示框:',
              message: "检测到存在自定义商品，已确认联系商家核对价格？",
              buttons: [{
                  label: '取消',
                  action: function(dialog) {
                      e.preventDefault();
                      dialog.close();
                  }
              }, {
                  label: '确定',
                  cssClass: 'btn-primary',
                  action: function(dialog){
  									dialog.close();
                    location.href = "/sales/store_orders/new?store_id="+getUrlParam("store_id")
                  }
              }]
          });
    }else {
      location.href = "/sales/store_orders/new?store_id="+getUrlParam("store_id")
    }

	})

})
